package com.changgou.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.pay.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeiXinPayServiceImpl implements WeixinPayService {
    //应用id
    @Value("${weixin.appid}")
    private String appid;
    //商户号
    @Value("${weixin.partner}")
    private String partner;
    //秘钥
    @Value("${weixin.partnerkey}")
    private String partnerkey;
    //回调地址
    @Value("${weixin.notifyurl}")
    private String notifyurl;

    /**
     * 创建二维码
     * @param parameterMap
     * @return
     */
    @Override
    public Map<String,String> createNative(Map<String, String> parameterMap) {
        try {
            //封装参数
            Map<String,String> paramMap = new HashMap<>();
            paramMap.put("appid",appid);
            paramMap.put("mch_id",partner);
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paramMap.put("body","畅购商城商品1");
            //订单号
            paramMap.put("out_trade_no",parameterMap.get("outtradeno"));
            //总金额,订单总金额，单位为分
            paramMap.put("total_fee",parameterMap.get("totalfee"));
            paramMap.put("spbill_create_ip","127.0.0.1");
            //交易结果回调通知地址
            paramMap.put("notify_url",notifyurl);
            //交易类型
            paramMap.put("trade_type","NATIVE");
            //获取自定义数据,包含rabbitmq信息
            String exchange = parameterMap.get("exchange");
            String routingkey = parameterMap.get("routingkey");
            HashMap<String, String> attachMap = new HashMap<>();
            attachMap.put("exchange",exchange);
            attachMap.put("routingkey",routingkey);
            //传递用户名
            String username = parameterMap.get("username");
            if(!StringUtils.isEmpty(username)){
                attachMap.put("username",username);
            }
            String attach = JSON.toJSONString(attachMap);
            paramMap.put("attach",attach);

            //转xml会自动生成签名
            String xmlParameters = WXPayUtil.generateSignedXml(paramMap,partnerkey);

            //微信提供的url
            String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            HttpClient httpClient = new HttpClient(url);
            httpClient.setHttps(true);
            httpClient.setXmlParam(xmlParameters);
            //提交请求
            httpClient.post();
            //返回xml数据
            String result = httpClient.getContent();
            Map<String,String> resultMap = WXPayUtil.xmlToMap(result);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询支付状态
     * @param parameterMap
     * @return
     */
    @Override
    public Map<String,String> queryPayStatus(String outtradeno) {
        try {
            //封装参数
            Map<String,String> paramMap = new HashMap<>();
            paramMap.put("appid",appid);
            paramMap.put("mch_id",partner);
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            //订单号
            paramMap.put("out_trade_no",outtradeno);
            //转xml会自动生成签名
            String xmlParameters = WXPayUtil.generateSignedXml(paramMap,partnerkey);

            //微信提供的url
            String url = "https://api.mch.weixin.qq.com/pay/orderquery";
            HttpClient httpClient = new HttpClient(url);
            httpClient.setHttps(true);
            httpClient.setXmlParam(xmlParameters);
            //提交请求
            httpClient.post();
            //返回xml数据
            String result = httpClient.getContent();
            Map<String,String> resultMap = WXPayUtil.xmlToMap(result);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
